using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Collections.ObjectModel;
using BodySoundSimulator.CaseData;

namespace BodySoundSimulator.AbsoluteMapData
{
	class SoundMapDataManipulator
	{
		internal static readonly Pen mapGridDrawingPen = new Pen(Color.FromArgb(80, Color.Red));
		internal static readonly Brush mapGridFillingBrush = new SolidBrush(Color.FromArgb(80, Color.Red));

		internal static void DrawFrontViewMap(Graphics g, SoundMapData mapData)
		{
			DrawMapGrid(g,
				mapData,
				mapData.FrontViewGridXCoordinates,
				mapData.FrontViewGridYCoordinates);
		}

		internal static void DrawBackViewMap(Graphics g, SoundMapData mapData)
		{
			DrawMapGrid(g,
				mapData,
				mapData.BackViewGridXCoordinates,
				mapData.BackViewGridYCoordinates);
		}

		internal static SoundMapCellInfo GetCellInfo(SoundMapData mapData,
			PointF measureingLocation,
			PatientView view)
		{
			switch (view)
			{
				case PatientView.FrontView:
					return GetCellInfo(mapData.FrontViewGridXCoordinates,
						mapData.FrontViewGridYCoordinates,
						measureingLocation);
				case PatientView.BackView:
					return GetCellInfo(mapData.BackViewGridXCoordinates,
						mapData.BackViewGridYCoordinates,
						measureingLocation);
					break;
				default:
					return null;
			}
		}

		internal static void ColorCell(Graphics g,
			SoundMapCellInfo cellInfo)
		{
			if (cellInfo != null)
			{
				g.FillRectangle(mapGridFillingBrush, cellInfo.Boundry);
			}
		}

		private static SoundMapCellInfo GetCellInfo(Collection<float> gridXCoordinates,
			Collection<float> gridYCoordinates,
			PointF measuringLocation)
		{
			float startX = 0;
			float endX = 0;
			float startY = 0;
			float endY = 0;

			int indexX = -1;
			int indexY = -1;
			for (int index = 0; index < gridXCoordinates.Count - 1; index++)
			{
				if (gridXCoordinates[index] <= measuringLocation.X && gridXCoordinates[index + 1] >= measuringLocation.X)
				{
					startX = gridXCoordinates[index];
					endX = gridXCoordinates[index + 1];
					indexX = index;
					break;
				}
			}
			for (int index = 0; index < gridYCoordinates.Count - 1; index++)
			{
				if (gridYCoordinates[index] <= measuringLocation.Y && gridYCoordinates[index + 1] >= measuringLocation.Y)
				{
					startY = gridYCoordinates[index];
					endY = gridYCoordinates[index + 1];
					indexY = index;
					break;
				}
			}
			if (indexX >= 0 && indexY >= 0)
			{
				SoundMapCellInfo cellInfo = new SoundMapCellInfo();
				cellInfo.Boundry = new RectangleF(startX, startY, endX - startX, endY - startY);
				cellInfo.CellID = indexX + indexY * (gridXCoordinates.Count - 1);
				return cellInfo;
			}
			else
			{
				return null;
			}
		}

		private static void DrawMapGrid(Graphics g,
			SoundMapData mapData,
			Collection<float> gridXCoordinates,
			Collection<float> gridYCoordinates)
		{
			float minX = gridXCoordinates[0];
			float maxX = gridXCoordinates[gridXCoordinates.Count - 1];
			float minY = gridYCoordinates[0];
			float maxY = gridYCoordinates[gridYCoordinates.Count - 1];
			foreach (float coordinateX in gridXCoordinates)
			{
				g.DrawLine(mapGridDrawingPen, coordinateX, minY, coordinateX, maxY);
			}
			foreach (float coordinateY in gridYCoordinates)
			{
				g.DrawLine(mapGridDrawingPen, minX, coordinateY, maxX, coordinateY);
			}
		}

	}
}
